﻿#region All usings
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using senSimulator.DrawableComponents.TrafficLights;

#endregion


namespace senSimulator.DrawableComponents.Vehicles
{
    class Pedestrian : Traffic
    {
        // The variables to control the pedestrian
        private bool _crossedTrafficLight         = false;
        private bool _hasReachedCenter            = false;
        private TrafficLight _secondTrafficLight  = null;
        

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="game">The main game class</param>
        /// <param name="textures">All textures</param>
        /// <param name="posX">Position X</param>
        /// <param name="posY">Position Y</param>
        /// <param name="centerX">Center X</param>
        /// <param name="centerY">Center Y</param>
        /// <param name="rotationInDegrees">Rotation in degrees</param>
        public Pedestrian(Game game, Texture2D textureName, Vector2 textureCenter, int posX, int posY, int rotationInDegrees, float speed, string from, string to)
            : base(game, textureName, textureCenter, posX, posY, rotationInDegrees, speed, from, to, "pedestrian")
        {
        }


        /// <summary>
        /// Crossed the traffic light (for movement)
        /// </summary>
        /// <returns>Boolean</returns>
        public bool CrossedTrafficLight()
        {
            return _crossedTrafficLight;
        }


        /// <summary>
        /// Set the parameter "Crossed the traffic light or not"
        /// </summary>
        /// <param name="crossed">Boolean</param>
        public void SetCrossedTrafficLight(bool crossed)
        {
            _crossedTrafficLight = crossed;
        }


        /// <summary>
        /// Get switched from traffic light
        /// </summary>
        /// <returns>Boolean</returns>
        public bool HasReachedCenter()
        {
            return _hasReachedCenter;
        }


        /// <summary>
        /// Set switched from traffic light
        /// </summary>
        /// <param name="switched">Switched</param>
        public void SetHasReachedCenter(bool switched)
        {
            _hasReachedCenter = switched; 
        }


        /// <summary>
        /// Get the second traffic light
        /// </summary>
        /// <returns>Second traffic light</returns>
        public TrafficLight GetSecondTrafficLight()
        {
            return _secondTrafficLight;
        }


        /// <summary>
        /// Store the second traffic light to move from
        /// </summary>
        /// <param name="secondTrafficLight"></param>
        public void SetSecondTrafficLight(TrafficLight secondTrafficLight)
        {
            _secondTrafficLight = secondTrafficLight;
        }


        /// <summary>
        /// Move the traffic based on the way points
        /// </summary>
        public override void MoveToWayPoint()
        {
            if (CalculateDistance(GetPosition(),_wayPoints[_currentWayPoint]) > _margin)
            {
                // Check which direction
                float xDistance = 0;
                float yDistance = 0;

                // Check if a temporary vector is set (for other traffic positions)
                if (_stopForTrafficVector != Vector2.Zero && GetPosition() != _stopForTrafficVector)
                {
                    xDistance = GetPosition().X - _stopForTrafficVector.X;
                    yDistance = GetPosition().Y - _stopForTrafficVector.Y;
                }
                else // Else drive towards the next position
                {
                    _stopForTrafficVector = Vector2.Zero;
                    xDistance = GetPosition().X - _wayPoints[_currentWayPoint].X;
                    yDistance = GetPosition().Y - _wayPoints[_currentWayPoint].Y;
                }

                float moveX = 0;
                float moveY = 0;
                if (xDistance > 0)
                    moveX = -_speed;
                else if (xDistance < 0)
                    moveX = _speed;

                if (yDistance > 0)
                    moveY -= _speed;
                else if (yDistance < 0)
                    moveY = _speed;
                SetPosition(moveX, moveY, true);

                // Set the angle
                _rotationInDegrees = _angleOffset + _preRenderedDirections[moveY.ToString() + moveX.ToString()];//(float)Math.Atan2(moveY, moveX);

                // Store the current move into the vector (for angle calculations)
                _currentMoveVector.X = moveX;
                _currentMoveVector.Y = moveY;
            }
            else
            {
                // If a check point has been reached, check if it's the end of the road or go to the next point
                if (_currentWayPoint < _wayPoints.Length - 1)
                    _currentWayPoint++;
                else
                {
                    // Else check if the pedestrian needs to switch from side
                    _finished = true;
                }
            }
        }
    }
}
